<?php

/**
 * Created by PhpStorm.
 * User: dx
 * Date: 2017/07/06
 * Time: 下午13:36
 * Description:友盟推送相关接口
 */
class UmengPushController extends BaseController{
    /**
     * @Author:      dx
     * @DateTime:    2017-07-06 13:42:29
     * @Description: 绑定 用户id
     */
    public function u_bind(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'device_token' =>'required',
                'device_type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }
            $res = UmengPushBind::bindDeviceToken($wechat_user->id,Input::get('device_token'),Input::get('device_type'));
            if(!$res){
                $response = Lang::get("error.u_meng_bing_error_91000");
                break;
            }
            $response = array('code' => 0,'msg' => 'ok','result' => array());
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "u_bind", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2017-07-06 17:35:29
     * @Description: 消息列表
     * 消息状态: 0-排队中, 1-发送中，2-发送完成，3-发送失败，4-消息被撤销，5-消息过期, 6-筛选结果为空，7-定时任务尚未开始处理
     */
    public function u_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            $res = UmengPushLog::where('status',UmengPushLog::STATUS_RECEIVED)->where('field_name','user_id')->where('field_value',$wechat_user->id)->orderBy('received_at','desc');
            if(Input::get('type')){
                $res->where('type',Input::get('type'));
            }
            $count = $res->count();
            $res=$res->skip($page_size*($page-1))->take($page_size)->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"u_list"=>$res));
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "u_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2017-07-13 13:50:29
     * @Description: 消息状态变更
     */
    public function u_read(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }
            $res = UmengPushLog::where('field_name','user_id')->where('field_value',$wechat_user->id)->where('type',Input::get('type'))->get();
            if($res){
                UmengPushLog::where("field_name",'user_id')->where("field_value",$wechat_user->id)->where("type",Input::get('type'))->update(array("is_read"=>Input::get('is_read',1),'updated_at'=>Common::currentTimeString()));
            }

            $response = array('code' => 0,'msg' => 'ok','result' => array());
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "u_read", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    public function u_demo(){
//        Orders::notifyUserOrderStatus(1000000165,'14891383805323','订单已确认','您的订单已确认，请准时到达门店哦。订单号:14891383805323');
        $coupon_base_info = CouponBaseInfo::where('id',144)->first();
        Orders::notifyMsg(1000000165,2,'优惠券已到账','您的'.($coupon_base_info->amount/100).'元优惠券已到帐');
    }
}